package br.com.fatecpg.fatecMobile;

import java.util.ArrayList;
import java.util.HashMap;

import br.com.fatecpg.sharepoint.SharepointList;
import br.com.fatecpg.utils.ApplicationUtils;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WarningsActivity extends Activity {

	ListView warningsListView = null;
	HashMap<String, ArrayList<String>> warningsMap = new HashMap<String, ArrayList<String>>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_warnings);

		ArrayList<String> attributes = new ArrayList<String>();
		attributes.add("ows_LinkTitle");
		attributes.add("ows_Modified");
		attributes.add("ows_Expires");
		attributes.add("ows_Created_x0020_Date");
		attributes.add("ows_Body");

		try {
			warningsMap = SharepointList
					.getList("Avisos", "10", "", attributes);
			ArrayList<String> list = warningsMap.get("ows_LinkTitle");
			warningsListView = (ListView) findViewById(R.id.warningsListView);
			ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, list);

			warningsListView.setAdapter(dataAdapter);

			warningsListView.setOnItemClickListener(new OnItemClickListener() {

				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					String title = warningsMap.get("ows_LinkTitle").get(
							position);
					String modified = ApplicationUtils.formatInverseDate(warningsMap.get("ows_Modified").get(
							position));
					String expires = ApplicationUtils.formatInverseDate(warningsMap.get("ows_Expires").get(
							position));
					String created = ApplicationUtils.formatInverseDate(ApplicationUtils.removeSharepointId(warningsMap.get("ows_Created_x0020_Date")
							.get(position)));
					String body = warningsMap.get("ows_Body").get(position);
					AlertDialog.Builder detalhes = new AlertDialog.Builder(
							WarningsActivity.this);
					detalhes.setTitle(title);
					detalhes.setMessage(ApplicationUtils.removeHtmlTags(body)
							.trim()
							+ "\n\n"
							+ "Criado em: "
							+ created
							+ "\n"
							+ "Modificado em: "
							+ modified
							+ "\nExpira em: "
							+ expires + "\n");
					detalhes.setNeutralButton("OK", null);
					detalhes.show();

				}

			});

		} catch (Exception ex) {
			ApplicationUtils.applicationSimpleDialog(WarningsActivity.this, "Erro", ex.getMessage());
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.warnings, menu);
		return true;
	}

}
